home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / BIGFIO.C < prev    next >
Text File  |  1993-06-26  |  2KB  |  108 lines

  1. /*
  2.     Large-buffer file I/O routines
  3.  
  4.     (W) Copywrong 1980 Scott W. Layson
  5.     All Rights Reversed.
  6.  
  7. These routines are almost-exact replacements for fopen, fcreat, putc,
  8. getc, ungetc and fflush, except that they allow use of an arbitrarily
  9. large (instead of always 128 bytes) buffer.  The program using them should
  10. declare the buffer as an array of [nb*128 + 8] chars, where nb is the
  11. number of 128-byte blocks the buffer is to contain.  FOpenBig and
  12. FCreatBig take nb as a third argument, but none of the other routines
  13. need it.
  14.  
  15. */
  16.  
  17.  
  18. struct buf {
  19.     int size, fd, nleft;    /* size is in blocks */
  20.     char *nextp, buff;    /* really buff[][128] */
  21.     };
  22.  
  23.  
  24. /* Note that this routine is called with the size, in blocks, of the buffer */
  25.  
  26. FOpenBig (filename, iobuf, buffsize)
  27. char *filename;
  28. struct buf *iobuf;
  29. int buffsize;
  30. {
  31.     if ( (iobuf->fd = open (filename, 0)) < 0) return -1;
  32.     iobuf->size = buffsize;
  33.     iobuf->nleft = 0;
  34.     return iobuf->fd;
  35. }
  36.  
  37.  
  38. FCreatBig (filename, iobuf, buffsize)
  39. char *filename;
  40. struct buf *iobuf;
  41. int buffsize;
  42. {
  43.     unlink (filename);
  44.     if ( (iobuf->fd = creat(filename)) < 0) return -1;
  45.     iobuf->size = buffsize;
  46.     iobuf->nleft = buffsize * 128;
  47.     iobuf->nextp = &(iobuf->buff);
  48. }
  49.  
  50.  
  51. GetcBig (iobuf)
  52. struct buf *iobuf;
  53. {
  54.     int nsecs;
  55.     if (iobuf == 0) return getchar();
  56.     if (iobuf->nleft--)  return *iobuf->nextp++;
  57.     if ( (nsecs = read (iobuf->fd, &(iobuf->buff), iobuf->size)) <= 0)
  58.         return -1;
  59.     iobuf->nleft = nsecs * 128 - 1;
  60.     iobuf->nextp = &(iobuf->buff);
  61.     return *iobuf->nextp++;
  62. }
  63.  
  64.  
  65. PutcBig (ochar, iobuf)
  66. char ochar;
  67. struct buf *iobuf;
  68. {
  69.     if (iobuf == 0)  {
  70.         putchar(ochar);
  71.         return 0;
  72.         }
  73.     if (iobuf->nleft == 0
  74.         && FflushBig(iobuf) < 0)  return -1;
  75.     --iobuf->nleft;
  76.     *iobuf->nextp++ = ochar;
  77.     return 0;
  78. }
  79.  
  80.  
  81. FflushBig (iobuf)
  82. struct buf *iobuf;
  83. {
  84.     if (iobuf == 0) return 0;
  85.     if (iobuf->nleft == iobuf->size * 128) return 0;
  86.     if ( write (iobuf->fd, &(iobuf->buff), iobuf->size - iobuf->nleft/128)
  87.         <=0) return -1;
  88.     if (iobuf->nleft != 0)
  89.         return seek (iobuf->fd, -iobuf->size, 1);
  90.     iobuf->nleft = iobuf->size * 128;
  91.     iobuf->nextp = & (iobuf->buff);
  92.     return 0;
  93. }
  94.  
  95.  
  96. UngetcBig(c,iobuf)
  97. char c;
  98. struct buf *iobuf;
  99. {
  100.     if (!iobuf)  {
  101.         ungetch(c);
  102.         return;
  103.      }
  104.     *--iobuf->nextp = c;
  105.     iobuf->nleft++;
  106. }
  107.  
  108.